package com.forlgb.powercloud.netty4.all.client;

import com.forlgb.powercloud.netty4.all.common.pojo.FileUploadFile;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import static com.forlgb.powercloud.netty4.all.common.util.PropertiesUtils.getProperties;

public class FileUploadClient {

    private static int port;

    private static String host;

    private static String path;



    /**
     * 建立与服务端的连接
     * @param port
     * @param host
     * @param fileUploadFile
     */
    public void connect(int port, String host, final FileUploadFile fileUploadFile) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<Channel>(){
                        @Override
                        protected void initChannel(Channel ch) throws Exception {
                            ch.pipeline().addLast(new ObjectEncoder());
                            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers
                                    .weakCachingConcurrentResolver(null)));
                            ch.pipeline().addLast(new FileUploadClientHandler(fileUploadFile));
                        }
                    });
            ChannelFuture f = bootstrap.connect(host, port).sync();
            f.channel().closeFuture().sync();
            System.out.println("FileUploadClient connect()结束");
        }finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws IOException {
        setBasicData();
        if (args != null && args.length > 0) {
            try {
                port = Integer.valueOf(args[0]);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        try {
            FileUploadFile uploadFile = new FileUploadFile();
            // d:/source.rar,D:/2014work/apache-maven-3.5.0-bin.tar.gz
            File file = new File(path);
            // 文件名
            String fileMd5 = file.getName();
            uploadFile.setFile(file);
            uploadFile.setFile_md5(fileMd5);
            // 文件开始位置
            uploadFile.setStarPos(0);
            new FileUploadClient().connect(port, host, uploadFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setBasicData() throws IOException {
        Properties properties = getProperties("/client.properties");
        host = String.valueOf(properties.get("tcp.client.ip"));
        port = Integer.valueOf(String.valueOf(properties.get("tcp.client.port")));
        path = String.valueOf(properties.get("tcp.client.path"));
    }
}
